'use strict';

const Service = require('egg').Service;

class ProjectService extends Service {
  //获取项目列表
  async get() {
      var projectData = await this.app.model.Project.findAll({
        order :[['id', 'ASC']]
      });
      return projectData    
  }
  //添加项目
  async create(){
    try{
      var { name,project_site,project_sketch,project_state } = this.ctx.request.body;
      await this.app.model.Project.create({
      name,project_site,project_sketch,project_state
      })
      return this.get()
    }catch(e){
      return e.message
    }
    
  }
  //修改项目
  async update(){
    try{
      var { name,project_site,project_sketch,project_state } = this.ctx.request.body;
      await this.app.model.Project.update({name,project_site,project_sketch,project_state},{where:{name:this.ctx.params.id}})
      return this.get();
    }catch(e){
      return e.message
    }
  }
  //删除项目

  async destroy(){
    try{
      await this.app.model.Project.destroy({
        where:{
            name:this.ctx.params.id
        }
      })
      return this.get();
    }catch(e){
      return e.message
    }
  }

}

module.exports = ProjectService;
